package com.example.demo.controller;

import com.example.common.entity.CommentInfo;
import com.example.common.entity.UserInfo;
import com.example.common.tools.AjaxResult;
import com.example.common.tools.UserSessionUtils;
import com.example.demo.service.ArticleService;
import com.example.demo.service.CommentService;
import com.example.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/comment")
public class CommentController {

    @Autowired
    private CommentService commentService;
    @Autowired
    private UserService userService;
    @Autowired
    private ArticleService articleService;

    @RequestMapping("/add")
    public AjaxResult add(HttpServletRequest request, CommentInfo commentInfo) {
        //非空校验
        if(commentInfo == null  || !StringUtils.hasLength(commentInfo.getComment())) {
            return AjaxResult.fail(403, "参数错误!");
        }
        //获取当前用户 id
        UserInfo userInfo = UserSessionUtils.getUser(request);
        if(userInfo == null || !StringUtils.hasLength(userInfo.getUsername()) ||
        !StringUtils.hasLength(userInfo.getPassword())) {
            return AjaxResult.fail(403, "请登录后评论!");
        }
        //设置 uid、nickname、photo 后存储评论数据
        commentInfo.setUid(userInfo.getId());
        commentInfo.setNickname(userInfo.getNickname());
        commentInfo.setPhoto(userInfo.getPhoto());
        return AjaxResult.success(commentService.add(commentInfo));
    }

    @RequestMapping("/show")
    public AjaxResult show(Integer aid) {
        //非空校验
        if(aid == null) {
            return AjaxResult.fail(403, "参数错误!");
        }
        //通过文章 id 获取所有该文章的评论
        return AjaxResult.success(commentService.showCommentByAid(aid));
    }

    @RequestMapping("/del")
    public AjaxResult del(HttpServletRequest request, Integer id, Integer autId) {
        //非空校验
        if(id == null || autId == null) {
            return AjaxResult.fail(403, "您无权删除该评论!");
        }

        //评论能被删除需要具备以下两个条件其中之一:
        //1.当前用户 id 等于 评论 uid
        //2.当前文章作者信息的 id 需要等于 当前用户的 id

        //获取当前用户信息
        UserInfo curUserInfo = UserSessionUtils.getUser(request);
        if(curUserInfo == null || !StringUtils.hasLength(curUserInfo.getUsername()) ||
        !StringUtils.hasLength(curUserInfo.getPassword())) {
            return AjaxResult.fail(403, "您无权删除该评论!");
        }
        //获取作者信息
        UserInfo autUserInfo = userService.getUserById(autId);
        if(autUserInfo == null || !StringUtils.hasLength(autUserInfo.getUsername()) ||
                !StringUtils.hasLength(autUserInfo.getPassword())) {
            return AjaxResult.fail(403, "您无权删除该评论!");
        }
        //获取评论信息
        CommentInfo commentInfo = commentService.getCommentInfoById(id);
        if(commentInfo == null || !StringUtils.hasLength(commentInfo.getNickname()) ||
                !StringUtils.hasLength(commentInfo.getComment())) {
            return AjaxResult.fail(403, "您无权删除该评论!");
        }

        //只要满足条件即可删除
        if(curUserInfo.getId().equals(commentInfo.getUid()) ||
        autUserInfo.getId().equals(curUserInfo.getId())) {
            //可以进行删除
            return AjaxResult.success(commentService.delCommentByid(id));
        } else {
            //不可以进行删除
            return AjaxResult.fail(403, "您无权删除该评论!");
        }
    }

}
